<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="files_handler.FileServlet"%>
<jsp:useBean id="user" class="serverDB_classes.UserData" scope="session"/> 
<%@ page language="java" %>
<%@ page import="java.io.*" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Display file upload form to the user</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<!--[if IE 6]>
	<script type="text/javascript" src="unitpngfix.js"></script>
<![endif]--> 
</head>
<BODY> 
<div class="wrap">
	<div class="header">
    </div>    
    <div class="tab_bg">        
        <div class="tab_left">
       <img src="images/big_icon.png" alt="" title="" border="0" class="big_icon" />       
        </div>
        
        <div class="middle_text">
<%
	files_handler.FileServlet fileServlet=
	//to get the content type information from JSP Request Header
	String contentType = request.getContentType();
	//here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0
	if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
 		
		DataInputStream in = new DataInputStream(request.getInputStream());
		FileOutputStream fileOut = new FileOutputStream("C:\\temp\\uploaded.rar");
		//we are taking the length of Content type data
		//int formDataLength = request.getContentLength();
		byte buffer[] = new byte[4096];
		int byteRead = 0;
		int totalBytesRead = 0;
		//this loop converting the uploaded file into byte code
		while ((byteRead = in.read(buffer))!=-1) {
			fileOut.write(buffer, 0, byteRead);			
			}
		

		String file = new String(buffer);
		//for saving the file name
		String saveFile = file.substring(file.indexOf("filename=\"") + 10);
		saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
		saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
		int lastIndex = contentType.lastIndexOf("=");
		String boundary = contentType.substring(lastIndex + 1,contentType.length());
		int pos;
		//extracting the index of file 
		pos = file.indexOf("filename=\"");
		pos = file.indexOf("\n", pos) + 1;
		pos = file.indexOf("\n", pos) + 1;
		pos = file.indexOf("\n", pos) + 1;
		int boundaryLocation = file.indexOf(boundary, pos) - 4;
		int startPos = ((file.substring(0, pos)).getBytes()).length;
		int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

		// creating a new file with the same name and writing the content in new file
		try{
			FileOutputStream fileOut = new FileOutputStream("C:\\temp\\uploaded.rar");
			fileOut.write(dataBytes, startPos, (endPos - startPos));
			fileOut.flush();
			fileOut.close();
			%><Br><br><br><table border="0"><tr><td><b>You have successfully upload the file:</b></td></tr>
			<tr><td><b><% out.println(saveFile); %></b></td></tr> 
			<tr><td><p><a href="files.jsp"><img border="0" src="images/upload1.png"></a></p></td></tr></table>
			<%			
		}
		catch (java.io.FileNotFoundException e){
			%><Br><br><br><table border="0"><tr><td><b>You didn't choose any file =\</b></td></tr>
			<tr><td><p><a href="files.jsp"><img border="0" src="images/upload1.png"></a></p></td></tr></table>
			<%			
		}
		catch (Exception e){
			%><Br><br><br><table border="0"><tr><td><b>fail to load file =\</b></td></tr>
			<tr><td><p><a href="files.jsp"><img border="0" src="images/upload1.png"></a></p></td></tr></table>
			<%			
		}
	}
%>

         </div>
    
    <div class="clear"></div>
    </div></div>



</BODY>

</HTML>